﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore.Metadata;
using System.Runtime.InteropServices;
using Models;

#nullable disable

namespace DAL
{
    public partial class ITAMContext : DbContext
    {
        public ITAMContext()
        {
        }

        public ITAMContext(DbContextOptions<ITAMContext> options)
            : base(options)
        {
        }

        public virtual DbSet<DepreciationRule> DepreciationRules { get; set; }
        public virtual DbSet<DeviceCategory> DeviceCategories { get; set; }
        public virtual DbSet<DeviceOwnership> DeviceOwnerships { get; set; }
        public virtual DbSet<DeviceRecord> DeviceRecords { get; set; }
        public virtual DbSet<PurchasedChannel> PurchasedChannels { get; set; }
        public virtual DbSet<Vendor> Vendors { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                string connString = "";
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    connString = AppConfigurationServices.Configuration.GetConnectionString("deepin");
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    connString = AppConfigurationServices.Configuration.GetConnectionString("win7");
                }
                optionsBuilder.UseMySql(connString, Microsoft.EntityFrameworkCore.ServerVersion.Parse("10.4.20-mariadb"));
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasCharSet("utf8mb4")
                .UseCollation("utf8mb4_general_ci");

            modelBuilder.Entity<DepreciationRule>(entity =>
            {
                entity.HasIndex(e => e.Name, "Name")
                    .IsUnique();

                entity.Property(e => e.Id).HasColumnType("int(10) unsigned");

                entity.Property(e => e.CreateAt).HasColumnType("timestamp");

                entity.Property(e => e.DeleteAt).HasColumnType("timestamp");

                entity.Property(e => e.Description)
                    .HasMaxLength(191)
                    .HasDefaultValueSql("''");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(20);

                entity.Property(e => e.Rule).IsRequired();

                entity.Property(e => e.UpdatedAt)
                    .HasColumnType("timestamp")
                    .HasDefaultValueSql("current_timestamp()");
            });

            modelBuilder.Entity<DeviceCategory>(entity =>
            {
                entity.Property(e => e.Id).HasColumnType("int(10) unsigned");

                entity.Property(e => e.CreateAt).HasColumnType("timestamp");

                entity.Property(e => e.DeleteAt).HasColumnType("timestamp");

                entity.Property(e => e.DepreciationRuleId)
                    .HasColumnType("int(11)")
                    .HasDefaultValueSql("'0'");

                entity.Property(e => e.Description)
                    .HasMaxLength(191)
                    .HasDefaultValueSql("''");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(20);

                entity.Property(e => e.ParentId)
                    .HasColumnType("int(10) unsigned")
                    .HasDefaultValueSql("'0'");

                entity.Property(e => e.UpdatedAt)
                    .HasColumnType("timestamp")
                    .HasDefaultValueSql("current_timestamp()");
            });

            modelBuilder.Entity<DeviceOwnership>(entity =>
            {
                entity.ToTable("DeviceOwnership");

                entity.Property(e => e.Id).HasColumnType("int(10) unsigned");

                entity.Property(e => e.CreateAt).HasColumnType("timestamp");

                entity.Property(e => e.DeleteAt).HasColumnType("timestamp");

                entity.Property(e => e.DeviceId).HasColumnType("int(10) unsigned");

                entity.Property(e => e.LendDescription).HasMaxLength(191);

                entity.Property(e => e.LendTime).HasColumnType("datetime");

                entity.Property(e => e.PlanReturnTime).HasColumnType("datetime");

                entity.Property(e => e.ReturnDescription).HasMaxLength(191);

                entity.Property(e => e.ReturnTime).HasColumnType("datetime");

                entity.Property(e => e.UpdatedAt)
                    .HasColumnType("timestamp")
                    .HasDefaultValueSql("current_timestamp()");

                entity.Property(e => e.UserId).HasColumnType("int(10) unsigned");
            });

            modelBuilder.Entity<DeviceRecord>(entity =>
            {
                entity.Property(e => e.Id).HasColumnType("int(10) unsigned");

                entity.Property(e => e.AssetNumber)
                    .IsRequired()
                    .HasMaxLength(191);

                entity.Property(e => e.CategoryId).HasColumnType("int(10) unsigned");

                entity.Property(e => e.CreateAt).HasColumnType("timestamp");

                entity.Property(e => e.DeleteAt).HasColumnType("timestamp");

                entity.Property(e => e.DepreciationRuleId)
                    .HasColumnType("int(10) unsigned")
                    .HasDefaultValueSql("'0'");

                entity.Property(e => e.Description)
                    .HasMaxLength(191)
                    .HasDefaultValueSql("''");

                entity.Property(e => e.ExpireDate).HasColumnType("date");

                entity.Property(e => e.Ip)
                    .HasMaxLength(191)
                    .HasColumnName("IP")
                    .HasDefaultValueSql("''");

                entity.Property(e => e.Mac)
                    .HasMaxLength(191)
                    .HasDefaultValueSql("''");

                entity.Property(e => e.Photo)
                    .HasMaxLength(191)
                    .HasDefaultValueSql("''");

                entity.Property(e => e.Price).HasColumnType("decimal(18, 2)").HasDefaultValueSql("'0'");

                entity.Property(e => e.PurchaseChannelId)
                    .HasColumnType("int(10) unsigned")
                    .HasDefaultValueSql("'0'");

                entity.Property(e => e.PurchasedDate).HasColumnType("date");

                entity.Property(e => e.State)
                    .HasMaxLength(10)
                    .HasDefaultValueSql("'闲置'");

                entity.Property(e => e.UpdatedAt)
                    .HasColumnType("timestamp")
                    .HasDefaultValueSql("current_timestamp()");

                entity.Property(e => e.VendorId).HasColumnType("int(10) unsigned");
            });

            modelBuilder.Entity<PurchasedChannel>(entity =>
            {
                entity.HasIndex(e => e.Name, "Name")
                    .IsUnique();

                entity.Property(e => e.Id).HasColumnType("int(10) unsigned");

                entity.Property(e => e.CreateAt).HasColumnType("timestamp");

                entity.Property(e => e.DeleteAt).HasColumnType("timestamp");

                entity.Property(e => e.Description)
                    .HasMaxLength(191)
                    .HasDefaultValueSql("''");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(30);

                entity.Property(e => e.UpdatedAt)
                    .HasColumnType("timestamp")
                    .HasDefaultValueSql("current_timestamp()");
            });

            modelBuilder.Entity<Vendor>(entity =>
            {
                entity.HasIndex(e => e.Name, "Name")
                    .IsUnique();

                entity.Property(e => e.Id).HasColumnType("int(10) unsigned");

                entity.Property(e => e.Contact)
                    .HasMaxLength(191)
                    .HasDefaultValueSql("''");

                entity.Property(e => e.CreateAt).HasColumnType("timestamp");

                entity.Property(e => e.DeleteAt).HasColumnType("timestamp");

                entity.Property(e => e.Description)
                    .HasMaxLength(191)
                    .HasDefaultValueSql("''");

                entity.Property(e => e.Location)
                    .HasMaxLength(20)
                    .HasDefaultValueSql("''");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(30);

                entity.Property(e => e.UpdatedAt)
                    .HasColumnType("timestamp")
                    .HasDefaultValueSql("current_timestamp()");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
